package com.xinlijiankang.web.dao;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository("operateDao")
public class OperateDaoImpl extends HibernateDaoSupport implements OperateDao {

	@Resource
    public void setSessionFacotry(SessionFactory sessionFacotry) {  
        super.setSessionFactory(sessionFacotry);  
    }
    
	@SuppressWarnings("unchecked")
	@Override
	public <T> List<T> findAll(Class<T> t) {
		List<T> list=this.getHibernateTemplate().find("from "+t.getName());
		return list;
	}

	@Override
	public <T> T findById(Class<T> t, Serializable id) {
		T tid=(T)this.getHibernateTemplate().get(t, id);
		return tid;
	}

	@Override
	public <T> void saveOperate(T t) {
		this.getHibernateTemplate().save(t);
		
	}

	@Override
	public <T> void updateOperate(T t) {
		this.getHibernateTemplate().update(t);
		
	}

	@Override
	public <T> void deleteOperate(T t) {
		if(t!=null)
			this.getHibernateTemplate().delete(t);
		
	}

	@Override
	public <T> void deleteOperate(Class<T> t,Serializable id) {
		deleteOperate(getSession().get(t, id));
	}

	@Override
	public <T> T findByName(Class<T> t, String str ,String qStr) {
		@SuppressWarnings("unchecked")
		List<T> list=this.getHibernateTemplate().find("from "+t.getName()+" t where t."+qStr+"=?",str);
		if (list.size()==0)
			return null;
		else return (T)list.get(0);
	}
}
